/*二. 猜数游戏(2人)等级：D
功能要求：计算机产生随机数，猜中即胜，猜不中，提示是大了还是小了，继续猜，直至猜到，给出所用时间和评语。保留用户测试成绩，做出成绩排行榜。排行榜存放到文件中。
*/
package guess;
import java.io.*;
import java.util.*;

public class guessnumber {
    private ArrayList<User> arrayList;
    public void judgeprocess() {
        Random ran = new Random();
        int x = ran.nextInt(100);
        User user = new User();
        int count = user.getCount();
        //int count = 0;
        long time1 = user.getTime();
        //long time1 = 0;
        long starttime = System.currentTimeMillis();
        while (true) {
            try {
                Scanner in = new Scanner(System.in);
                int y = in.nextInt();
                if (y < 1 || y > 100) {
                    System.out.println("超出范围");
                } else if (y > x) {
                    System.out.println(x + "太大了");
                    count++;
                } else if (y < x) {
                    System.out.println(x + "太小了");
                    count++;
                } else {
                    long endtime = System.currentTimeMillis();
                    long time = (endtime - starttime) / 1000;
                    System.out.println("猜对了,共猜了" + count + "次，用时为" + time + "秒");
                    time1 = time;
                    break;
                }
            } catch (Exception e) {
                System.out.println("数字格式不合法");
                continue;
            }
        }
        System.out.println("输入姓名：");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        try {
            /*File file = new File("D:\\data.txt");
            if(!file.exists()) {
                file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
            }
            FileOutputStream fos = new FileOutputStream(file,true);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(name + "\t" + count + "\t" + time1 + "\t");
            bw.newLine();
            bw.flush();
            bw.close();
            osw.close();
            fos.close();*/
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\data.txt", true));
            bufferedWriter.write(name + "\t" + count + "\t" + time1 + "\t"+"\n");
            bufferedWriter.close();
            System.out.println("成绩排行榜已保存！");
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        System.out.println("---------成绩排行榜---------");
        File file = new File("D:\\data.txt");
        String result = "";
        class UserComparator implements Comparator<User> {
            @Override
            public int compare(User o1, User o2) {
                // TODO Auto-generated method stub
                long time01= o1.getTime();
                long time02= o2.getTime();
                int count1 = o1.getCount();
                int count2 = o2.getCount();
                if (time01 < time02) {
                    return -1;
                } else if (time01 == time01) {
                    if(count1<count2){
                        return -1;
                    }else if(count1 == count2){
                        return 0;
                    }else{
                        return 1;
                    }
                } else {
                    return 1;
                }
            }
        }
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while((line=br.readLine())!=null){
                System.out.println(line+"\n");
            }
            br.close();
            //BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\rankdata.txt", true));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}